
<script setup lang="ts">
/**
 * @description iconfont组件
 * @param iconName { String } 图标名称，对应iconfont的文字
 * @param className { String } 图标class值，对应css class
 * @param color { String } 图标颜色，对应css颜色
 * @example
 *  <SvgIcon iconName="icon-zhifubao1" color="#1890ff" className="alibaba"/>
 */
import { computed, ComputedRef, withDefaults } from 'vue';
interface SvgIconProps {
  iconName: string,
  className?: string,
  color?: string,
  size?: number | string,
  width?: number | string,
  height?: number | string
}
const props = withDefaults(defineProps<SvgIconProps>(), {
  width: '1em',
  height: '1em',
  color: 'var("--el-text-color-primary")'
})
// 图标在 iconfont 中的名字
const iconClassName:ComputedRef<string> = computed(():string =>{
  return `#icon-${props.iconName}`;
})
// 给图标添加上类名
const svgClass:ComputedRef<string> = computed((): string => {
  if (props.className) {
    return `svg-icon ${props.className}`;
  }
  return 'svg-icon';
});
</script>

<template>
  <svg :class="svgClass" aria-hidden="true" :style="{ width: size || width, height: size || height }">
    <use :xlink:href="iconClassName" :fill="color" />
  </svg>
</template>

<style scoped>
.svg-icon {
  position: relative;
  fill: currentColor;
  vertical-align: -2px;
  display: inline-block;
}
</style>